package com.mall.shopping.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.mall.shopping.IContentService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.services.cache.CacheManager;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static com.mall.shopping.constant.GlobalConstants.HEADER_PANEL_CACHE_KEY;
import static com.mall.shopping.constant.GlobalConstants.HEADER_PANEL_ID;

/**
 * @program: cs-mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-21 19:25
 **/
@Slf4j
@Component
@Service
public class ContentServiceImpl implements IContentService {
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    CacheManager cacheManager;
    @Override
    public NavListResponse queryNavList() {
        NavListResponse response = new NavListResponse();
        ObjectMapper mapper = new ObjectMapper();
        try {
            if(cacheManager.checkCache(HEADER_PANEL_CACHE_KEY)==null) {
                Example example = new Example(PanelContent.class);
                example.createCriteria().andEqualTo("panelId", HEADER_PANEL_ID);
                // 查询导航栏板块
                List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
                List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
                response.setPannelContentDtos(panelContentDtos);
                // json
                String value = mapper.writeValueAsString(panelContentDtos);
                cacheManager.setCache(HEADER_PANEL_CACHE_KEY,value,1);

                System.out.println("从数据库中获取的"+panelContentDtos.getClass().getName());
            }else {
                String cache = cacheManager.checkCache(HEADER_PANEL_CACHE_KEY);
                TypeFactory typeFactory = mapper.getTypeFactory();
                CollectionType collectionType = typeFactory.constructCollectionType(List.class, PanelContentDto.class);

                List<PanelContentDto> panelContentDtos = mapper.readValue(cache, collectionType);

                response.setPannelContentDtos(panelContentDtos);
                System.out.println("从redis中获取的"+panelContentDtos.getClass().getName());
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("ContentServiceImpl.queryNavList occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
